package com.example.file_20210629;

import java.io.File;

public class DiZGuiTest {

    public static void main(String[] args) {
        // 求任意一个数，等差至1的和 例：3= 3+2+1 2=2+1 1=1
//        int num = getNum(4);
//        System.out.println("num="+num);
        // 递归打印example下面所有文件
        File file = new File("D:\\PuyuanWork\\WorkGit_NW\\demo\\src\\main\\java\\com");
//        System.out.println(file.isDirectory());
//        System.out.println(file.getAbsolutePath());
        getAllFileName(file,0);
    }
    private static int getNum(int n){
        if(n < 2){
            return 1;
        }
        return n+getNum(n-1);
    }

    private static void getAllFileName(File file,int level){
        if(level > 0){
            printSpace(level);
            for (int i = 0; i < level; i++) {
                if(i <= 0){
                    System.out.print("|-");
                }else{
                    System.out.print("-");
                }
            }
        }else{
            System.out.print("根目录:");
        }
        if(file.isDirectory()){
            File[] files = file.listFiles();
            System.out.println(file.getName()+"文件目录ˇ");
            if(files != null){
                for (File file1:files) {
                    getAllFileName(file1,level+1);
                }
            }
        }else{
            System.out.println(file.getName());
        }
    }

    private static void printSpace(int levle){
        StringBuffer stringBuffer = new StringBuffer();
        for (int i = 0; i < levle ; i++) {
            stringBuffer.append("  ");
        }
        System.out.print(stringBuffer);
    }
}
